<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\yperp\home;

use app\yperp\common\PhpWordTemplateProcessor;
use Fairy\Html2WordMaker;
use PhpOffice\PhpWord\ComplexType\TblWidth;
use PhpOffice\PhpWord\Shared\Converter;
use think\Controller;
use think\Exception;
use PhpOffice\PhpWord\Element\Cell;
use PhpOffice\PhpWord\Element\Table;
use PhpOffice\PhpWord\Element\Row;

use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Shared\Html;
use PhpOffice\PhpWord\Element\Link;

/**
 * 前台首页控制器
 * @package app\cms\admin
 */
class Index extends Controller
{
    /**
     * 首页
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     */
    public function index()
    {

        $data = [
            'content' => [
                'id' => 124,
                'title' => '测试标题',
                'name' => '测试名称',
                'create_time' => '2024-02-29 19:22:37',
                'phone' => '123456789',
                'type' => '测试人员',
                'content' => '测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容测试内容',
            ],
            'images' => [
                env('root_path')   . 'public' . get_file_path(1),
                env('root_path')   . 'public' . get_file_path(2),
                env('root_path')   . 'public' . get_file_path(3),
                env('root_path')   . 'public' . get_file_path(3),
            ],
            'files' => [
                env('root_path')   . 'public' . get_file_path(1),
                env('root_path')   . 'public' . get_file_path(2),
                env('root_path')   . 'public' . get_file_path(3),
            ],
            'reply' => [
                ['reply_type' => '学校', 'reply_content' => '学校回复内容', 'reply_time' => '2024-04-25'],
                ['reply_type' => '群众', 'reply_content' => '群众回复内容', 'reply_time' => '2024-04-26'],
                ['reply_type' => '群众', 'reply_content' => '群众回复内容', 'reply_time' => '2024-04-26'],

            ]
        ];

        $save_path = env('root_path') . 'runtime/demo.docx';
        PhpWordTemplateProcessor::todo($data, $save_path);










        die;
        // return $this->fetch(); // 渲染模板




        //模板文件
        $save_path = env('root_path') . 'runtime/demo.docx';
        // dump($imaes);
        // dump($save_path);
        // die;
        //1.创建模板对象
        // $templateProcessor = new PhpWordTemplateProcessor($save_path);
        $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($save_path);

        //2.插入数据
        //插入文字
        $templateProcessor->setValue('id', '1');
        $templateProcessor->setValue('title', '123123');
        $templateProcessor->setValue('time', '2023年7月11日10:00--18:00');
        $templateProcessor->setValue('name', '王某某');
        $templateProcessor->setValue('phone', '15760010100');
        $templateProcessor->setValue('type', '特种作业人员');
        $templateProcessor->setValue('content', '西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某西藏某某');


        //插入图片，宽200像素，高300像素
        // $picParam = ['path' => env('root_path')   . 'public' . get_file_path(1), 'width' => 150, 'height' => 200];
        // $templateProcessor->setImageValue('picurl', $picParam);

        $templateProcessor->setValue('url', 'http://www.example.com', true);

        $imaes = [
            env('root_path')   . 'public' . get_file_path(1),
            env('root_path')   . 'public' . get_file_path(2),
            env('root_path')   . 'public' . get_file_path(3),
            env('root_path')   . 'public' . get_file_path(4),

        ];

        $templateProcessor->cloneRow('images', count($imaes));

        foreach ($imaes as $i => $item) {

            $templateProcessor->setImageValue(sprintf('images#%d', $i + 1), $item);
        }



        $files = [
            'https://github.com/PHPOffice/PHPWord',
            'https://github.com/PHPOffice/PHPWord',
            'https://github.com/PHPOffice/PHPWord',
            'https://github.com/PHPOffice/PHPWord',
            'https://github.com/PHPOffice/PHPWord',
        ];
        $templateProcessor->cloneRow('files', count($files));

        foreach ($files as $i => $link) {

            $templateProcessor->setValue(sprintf('files#%d', $i + 1), $link);
        }

        //检测目录，没有则创建
        $file_path   = env('root_path') . 'runtime/file.docx';

        //图片保存到服务器
        // $templateProcessor->saveAs($file_path);


        // $templateProcessor->save('hello_world.docx');

        // $templateProcessor->save();

        // header("Content-Disposition: attachment; filename=hello_world.docx");
        // readfile("hello_world.docx");





        $filename = iconv("utf-8", "gb2312", "测试");   //防止有的浏览器下载之后中文乱码

        header("Content-type: application/vnd.ms-word");
        header("Content-Disposition:attachment;filename=" . $filename . ".docx");
        header('Cache-Control: max-age=0');
        $templateProcessor->saveAs("php://output");
    }

    protected function getFiles($id_str)
    {
        $ids = explode(',', $id_str);
        if (is_array($ids)) {
            $new = [];
            foreach ($ids as $id) {
                $new[] =   env('root_path')   . 'public' . get_file_path($id);
            }
            return $new;
        } else {
            return [];
        }
    }
    /**
     * 首页
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     */
    public function app()
    {
        // Html2WordMaker::getInstance()->addFile(env('root_path') . 'public/test.html')->fetchImg('https://erp.lhp')->download("这是一个名称");

        // echo 'Word文档已生成。';
        $content = file_get_contents(env('root_path') . 'public/test.html');
        $filename = 'test';
        header('pragma:public');
        header('Content-type:application/vnd.ms-word;charset=utf-8;name="' . $filename . '".doc');
        header("Content-Disposition:attachment;filename=$filename.doc"); //attachment新窗口打印inline本窗口打印
        $html = '<html xmlns:o="urn:schemas-microsoft-com:office:office"
       xmlns:w="urn:schemas-microsoft-com:office:word"
         xmlns="http://www.w3.org/TR/REC-html40"><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'; //这句不能少，否则不能识别图片
        echo $html . $content . '</html>';
        die;
    }
}
